﻿#include "picker.h"

Picker::Picker(QWidget* parent)
	: QWidget(parent),
	is_leave(false),
	pickable_count(5),
	selected_shape(nullptr)
{
	for (int i = 0; i < 11; i++)
		pickable_shape.push_back(new RhomboidShape(this));
	pickable_shape.push_back(new RectShape(this));
	pickable_shape.push_back(new RoundedRectShape(this));
	pickable_shape.push_back(new CloudShape(this));
	pickable_shape.push_back(new CircleShape(this));
}

Shape* Picker::getSelectShape() const
{
	return selected_shape;
}

Shape* Picker::getShape(ShapeType type)
{
	switch (type)
	{
	case ShapeType::Rhomboid:
		return new RhomboidShape;
	case ShapeType::Rectangle:
		return new RectShape;
	case ShapeType::RoundedRect:
		return new RoundedRectShape;
	case ShapeType::Circle:
		return new CircleShape;
	case ShapeType::Cloud:
		return new CloudShape;
	default:
		break;
	}
	return nullptr;
}

void Picker::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	QStyleOption opt;
	opt.initFrom(this);
	style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
	for (auto shape : pickable_shape)
	{
		shape->paintPixmap(painter);
	}
	if (selected_shape != nullptr)
	{
		selected_shape->paintPixmap(painter);
	}

}

void Picker::resizeEvent(QResizeEvent* event)
{
	//同一行内图形间隔
	pickable_space = 10;
	//计算单个图片宽度
	pickable_width = (this->width() - (pickable_count - 1) * pickable_space) / pickable_count;
	//使用相同宽度高度
	int index = 0;
	for (auto shape : pickable_shape)
	{
		shape->setStartPoint(QPoint((index % 5) * (pickable_width + pickable_space), (index / 5) * (pickable_width + pickable_space) + (pickable_width - pickable_width / shape->getRate()) / 2));
		shape->setShapeWidth(pickable_width);
		shape->setShapeHeight(pickable_width / shape->getRate());
		index++;
	}
}

void Picker::mousePressEvent(QMouseEvent* event)
{
	for (auto shape : pickable_shape)
	{
		if (shape->clickDetect(QPoint(event->localPos().x(), event->localPos().y())))
		{
			selected_shape = Picker::getShape(shape->getType());
			selected_shape->setParent(this);
			selected_shape->setStartPoint(QPoint(event->localPos().x() - shape->getWidth(), event->localPos().y() - shape->getHeight()));
			selected_shape->setShapeWidth(shape->getWidth() * 2);
			selected_shape->setShapeHeight(shape->getHeight() * 2);
			update();
			return;
		}
	}
	QWidget::mousePressEvent(event);
}

void Picker::mouseMoveEvent(QMouseEvent* event)
{
	if (selected_shape != nullptr)
	{
		selected_shape->setStartPoint(QPoint(event->localPos().x() - pickable_width, event->localPos().y() - pickable_width / selected_shape->getRate()));
		//判断图形是否有一半超出picker
		if (!is_leave)
		{
			if (selected_shape->getMinX() + selected_shape->getMaxX() >= width() * 2)
			{
				emit startCreate();
				is_leave = true;
			}
		}
		if (is_leave)
		{
			if (!rect().contains(QPoint(event->localPos().x(), event->localPos().y())))
			{
				//发送的应该是不基于组件自身的全局坐标，然后在画布内再将全局坐标映射为画布本地坐标
				emit resetCreate(event->globalPos());
			}
		}
		update();
	}
	QWidget::mouseMoveEvent(event);
}

void Picker::mouseReleaseEvent(QMouseEvent* event)
{

	if (selected_shape != nullptr)
	{
		if (is_leave && selected_shape->getMinX() + selected_shape->getMaxX() >= width() * 2)
			emit finalCreate();
		else
			emit pauseCreate();
		delete selected_shape;
		selected_shape = nullptr;
	}
	is_leave = false;
	update();
	QWidget::mouseReleaseEvent(event);
}
